/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserManage;

import Medicine.Medicine;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import main.ketnoi;

/**
 *
 * @author ADMIN
 */
public class User extends javax.swing.JPanel {

    ketnoi a = new ketnoi();
    Connection con = a.connect();
    /**
     * Creates new form UserList
     */
    DefaultTableModel model;

    /**
     * Creates new form User
     */
    public User() {
        try {
            initComponents();
            model = (DefaultTableModel) tbUser.getModel();
            cbtype.addItem("----Select Type----");

            PreparedStatement pst = con.prepareStatement("select usertypename from usertypes");
            ResultSet re = pst.executeQuery();
            while (re.next()) {
                cbtype.addItem(re.getString(1));
            }
            LoadData();
            //Load cho combo search
            jComboBox1.addItem("---Select----");
            PreparedStatement pre1 = con.prepareStatement("Select * from information_schema.columns where table_name = 'Users'");
            ResultSet re1 = pre1.executeQuery();
            while (re1.next()) {
                jComboBox1.addItem(re1.getString(4));
            }
            //code auto for user
            txtc.setEnabled(false);
            int num1 = tbUser.getRowCount();
            txtc.setText("UC" + (num1 + 1));

        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void LoadData() {
        try {
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = stmt.executeQuery("Select * from Users");
            while (rs.next()) {
                Vector b = new Vector();
                b.add(rs.getString(1));
                b.add(rs.getString(2));
                b.add(rs.getString(3));
                b.add(rs.getString(4));
                model.addRow(b);
            }

            tbUser.setModel(model);
        } catch (SQLException ex) {
            Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reset() {
        txtc.setText("");
        txtn.setText("");
        txtp.setText("");
        cbtype.setSelectedIndex(0);
        txtc.requestFocus();
        //code auto for user
        txtc.setEnabled(false);
        int num1 = tbUser.getRowCount();
        txtc.setText("UC" + (num1 + 1));
    }

    public boolean checkUserCode() {
        try {
            if (txtc.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "UserCode invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtc.setText("");
                txtc.requestFocus();
                return false;
            }
            PreparedStatement pre = con.prepareStatement("select UserCode from Users");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                if (txtc.getText().equals(re.getString(1))) {
                    JOptionPane.showMessageDialog(this, "UserCode existed!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    //code auto for CustomerCode
                    txtc.setEnabled(false);
                    int num1 = tbUser.getRowCount();
                    Calendar now = Calendar.getInstance();
                    txtc.setText("UC" + (num1 + 1));
                    return false;
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public boolean checkUser() {
        if (txtn.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "UserName invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtn.setText("");
            txtn.requestFocus();
            return false;
        } else if (txtp.getText().length() < 6) {
            JOptionPane.showMessageDialog(this, "Password must have atleast 6 characters", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtp.setText("");
            txtp.requestFocus();
            return false;
        } else if (cbtype.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "UserType invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbUser = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        cbtype = new javax.swing.JComboBox();
        btnadd = new javax.swing.JButton();
        btndel = new javax.swing.JButton();
        btnupdate = new javax.swing.JButton();
        btnreset = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtp = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtc = new javax.swing.JTextField();
        txtn = new javax.swing.JTextField();

        setBackground(new java.awt.Color(231, 231, 231));
        setPreferredSize(new java.awt.Dimension(777, 702));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "UserList", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        tbUser.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "UserCode", "UserName", "Password", "UserTypeCode"
            }
        ));
        tbUser.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbUserMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbUser);

        jButton1.setText("Sort");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/search2.png"))); // NOI18N
        jButton2.setText("Search");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 481, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(189, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "UserDetail", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        cbtype.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbtypeActionPerformed(evt);
            }
        });

        btnadd.setText("Add");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btndel.setText("Delete");
        btndel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndelActionPerformed(evt);
            }
        });

        btnupdate.setText("Update");
        btnupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnupdateActionPerformed(evt);
            }
        });

        btnreset.setText("New");
        btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetActionPerformed(evt);
            }
        });

        jLabel4.setText("User Type Code");

        jLabel5.setText("Password");

        jLabel3.setText("User Code");

        jLabel2.setText("User Name");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtc)
                    .addComponent(txtn)
                    .addComponent(txtp)
                    .addComponent(cbtype, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(120, 120, 120)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnadd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btndel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnupdate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnreset, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(180, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbtype, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnadd))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btndel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnupdate))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnreset))))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(269, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tbUserMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbUserMouseClicked
        // TODO add your handling code here:
        btnadd.setEnabled(false);
        int r = tbUser.getSelectedRow();
        if (r < 0) {
            return;
        }
        txtc.setText(tbUser.getValueAt(r, 0).toString());
        txtn.setText(tbUser.getValueAt(r, 1).toString());
        txtp.setText(tbUser.getValueAt(r, 2).toString());
        if (tbUser.getValueAt(r, 3).equals("M")) {
            cbtype.setSelectedItem("Manager");
        } else {
            cbtype.setSelectedItem("Seller");
        }
    }//GEN-LAST:event_tbUserMouseClicked

    private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnresetActionPerformed
        // TODO add your handling code here:
        this.reset();
        btnadd.setEnabled(true);
    }//GEN-LAST:event_btnresetActionPerformed

    private void btndelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndelActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "     Are you sure? ", "---DELETE---", JOptionPane.YES_NO_OPTION, 2);
        if (n == 0) {
            try {
                // TODO add your handling code here:
                String str = null;
                PreparedStatement prst2 = con.prepareStatement("Delete from users where usercode = ?");
                str = tbUser.getValueAt(tbUser.getSelectedRow(), 0).toString();
                prst2.setString(1, str);
                prst2.executeUpdate();
                model.removeRow(tbUser.getSelectedRow());

                this.reset();
            } catch (SQLException ex) {
                Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btndelActionPerformed

    public boolean checkUpdate() {
//        try {
        if (txtc.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please select User from table above");
            return false;
        }
//            PreparedStatement pre = con.prepareStatement("select UserCode from Users");
//            ResultSet re = pre.executeQuery();
//            while (re.next()) {
//                if (txtc.getText() != (re.getString(1))) {
//                    JOptionPane.showMessageDialog(this, "Please check UserCode was selected");
//                    return false;
//                }
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return true;
    }
    private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnupdateActionPerformed
        if (checkUpdate() == true) {
            try {
                // TODO add your handling code here:
                PreparedStatement prst = con.prepareStatement("UPDATE users SET username=?,password=?,usertypecode=? WHERE usercode=?");
                prst.setString(1, txtn.getText());
                prst.setString(2, txtp.getText());
                if (cbtype.getSelectedItem().toString().equals("Manager")) {
                    prst.setString(3, "M");
                } else {
                    prst.setString(3, "S");
                }
                prst.setString(4, txtc.getText());
                prst.executeUpdate();

                //Load lai
                int num = tbUser.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }

                this.LoadData();
                this.reset();
            } catch (SQLException ex) {
                Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnupdateActionPerformed

    private void cbtypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbtypeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbtypeActionPerformed

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        if (checkUserCode() == true) {
            if (checkUser() == true) {
                try {
                    // TODO add your handling code here:
                    String a = null;
                    if (cbtype.getSelectedItem().toString().equals("Manager")) {
                        a = "M";
                    } else {
                        a = "S";
                    }
                    PreparedStatement prst = con.prepareStatement("insert into users values (?,?,?,?)");
                    prst.setString(1, txtc.getText());
                    prst.setString(2, txtn.getText());
                    prst.setString(3, txtp.getText());
//                    prst.setString(3, txtp.getPassword().toString());
                    System.out.println(cbtype.getSelectedItem().toString());
                    prst.setString(4, a);
                    prst.executeUpdate();


                    int num = tbUser.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }
                    this.LoadData();
                    this.reset();
                } catch (SQLException ex) {
                    Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnaddActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jComboBox1.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select sort condition!");
        } else {
            try {
                // TODO add your handling code here:
                //xoa bang
                int num = tbUser.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                //sapxep
                String str = jComboBox1.getSelectedItem().toString();
                PreparedStatement pst = con.prepareStatement("Select * from Users order by " + str);

                ResultSet rs1 = pst.executeQuery();
                Vector e = null;
                while (rs1.next()) {
                    e = new Vector();
                    e.add(rs1.getString(1));
                    e.add(rs1.getString(2));
                    e.add(rs1.getString(3));
                    e.add(rs1.getString(4));
                    model.addRow(e);
                }
            } catch (SQLException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (jComboBox1.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select search condition!");
        } else {
            try {
                // TODO add your handling code here:
                //Xoa bang
                int num = tbUser.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                //search
                String str1 = jComboBox1.getSelectedItem().toString();
                String str = jTextField1.getText();
                PreparedStatement pst = con.prepareStatement("Select * from Users where " + str1 + " = ?");
                pst.setString(1, str);
                ResultSet rs1 = pst.executeQuery();
                Vector e = null;
                while (rs1.next()) {
                    e = new Vector();
                    e.add(rs1.getString(1));
                    e.add(rs1.getString(2));
                    e.add(rs1.getString(3));
                    e.add(rs1.getString(4));
                    model.addRow(e);
                }
            } catch (SQLException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btndel;
    private javax.swing.JButton btnreset;
    private javax.swing.JButton btnupdate;
    private javax.swing.JComboBox cbtype;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tbUser;
    private javax.swing.JTextField txtc;
    private javax.swing.JTextField txtn;
    private javax.swing.JPasswordField txtp;
    // End of variables declaration//GEN-END:variables
}
